package com.winter.storm;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class Test {

    @SuppressWarnings("finally")
    public static final String test() {
        String t = "";

        try {
            t = "try";return t;
        } catch (Exception e) {
            t = "catch";
            return t;
        } finally {
            t = "finally";
            String.valueOf(null);
            return t;
        }
    }

    public static void main(String[] args) {
        int str = 1234567;
        System.out.println(reverse(str));
    }


    public static String reverse(int arg) {
        String val = String.valueOf(arg);
        char first = val.charAt(0);
        String addtion = val.substring(1);
        String result = addtion.length() == 1 ? addtion : reverse(Integer.parseInt(addtion));
        return result + first;
    }


    public static void testReadFile() {
        try {
            File file = new File("e:/a.txt");
            RandomAccessFile fi = new RandomAccessFile(file, "rw");
            FileChannel fc = fi.getChannel();


            ByteBuffer buffer = ByteBuffer.allocate((int) fc.size());
            fc.read(buffer);
            buffer.flip();
            String content = new String(buffer.array());
            System.out.println(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void testCopyFile() {
        try {
            File file = new File("e:/info.log");
            RandomAccessFile fi = new RandomAccessFile(file, "rw");

            File bFile = new File("e:/b.txt");
            RandomAccessFile bi = new RandomAccessFile(bFile, "rw");

            FileChannel fc = fi.getChannel();
            FileChannel bfc = bi.getChannel();

            ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);

            int len;
            int i = 1;
            while ((len = fc.read(buffer)) != -1) {
                System.out.println("Times:" + i);
                buffer.flip();
                bfc.write(buffer);
                buffer.clear();
                i++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void testIntBuffer() {
        IntBuffer buf = IntBuffer.allocate(10);//设置缓冲区容量为20个大小
        System.out.println("缓冲区写入数据之前：");
        System.out.println(buf.capacity());//调用capacity方法返回buf容量
        System.out.println(buf.position());//调用position方法返回buf的位置
        System.out.println(buf.limit());//调用limit方法返回buf的限制
        int temp[]={1,2,3,4};
        int i = 0;
        buf.put(i);//写入i
        buf.put(temp);//写入数组temp
        System.out.println("缓冲区写入数据之后：");
        System.out.println(buf.capacity());//调用capacity方法返回buf容量
        System.out.println(buf.position());//调用position方法返回buf的位置
        System.out.println(buf.limit());//调用limit方法返回buf的限制
        System.out.println("缓冲区内容：");
        buf.flip();//反转此缓冲区
        //输出缓冲区内容
        while(buf.hasRemaining()){
            int x = buf.get();//相对 get 方法。读取此缓冲区当前位置的 int，然后该位置递增。
            System.out.println(x);
        }
    }

    public static void testScanner() {
        try {
            InputStream fin = new FileInputStream(new File("E:/a.txt"));
            //InputStream fin = System.in;
            Scanner cin = new Scanner(fin);
            while (cin.hasNextLine()) {
                String m = cin.nextLine();
                System.out.println("结果：" + m);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void testNio() {
        try {
            File file = new File("e://a.txt");
            RandomAccessFile aFile = new RandomAccessFile(file, "rw");
            FileChannel channel = aFile.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(512);
            int bytesRead = channel.read(buffer);
            buffer.flip();

            Charset charset = Charset.defaultCharset();
            CharsetDecoder decoder = charset.newDecoder();
            CharBuffer charBuffer = decoder.decode(buffer);
            System.out.println(charBuffer.toString());

            buffer.clear();
            String newData = "New String to write to file\n";
            buffer.put(newData.getBytes());
            buffer.flip();
            while(buffer.hasRemaining()) {
                channel.write(buffer);
            }

            channel.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

